package com.kedsoft.docms.common.exception;

import com.kedsoft.docms.util.JsonUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * Description:自定义统一错误控制器
 *
 * @author Jin
 * @create 2017-04-09
 **/
@ControllerAdvice
public class DefaultExceptionHandler {
    /**
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ModelAndView CmsException(Exception e, HttpServletRequest request, HttpServletResponse response){
        return this.renderErrorView(404,"Not Found",e.getMessage());
    }

    private ModelAndView renderErrorView(Integer errorCode, String error, String message){
        ModelAndView view =new ModelAndView("error");
        view.addObject("status",errorCode);
        view.addObject("error",error);
        view.addObject("message",message);
        view.addObject("timestamp",new Date());
        return view;
    }

}
